//
//  ViewController.m
//  sasa
//
//  Created by Zzzzn on 16/5/11.
//  Copyright © 2016年 Zzzzn. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize button,label,string,num1,num2,num3,num4,num5;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建标签
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(25, 40, 265, 50)];
    self.label.textColor=[UIColor whiteColor];
    self.label.backgroundColor=[UIColor blackColor];
    self.label.textAlignment=UITextAlignmentRight;
    self.label.font=[UIFont systemFontOfSize:32.4];
    [self.view addSubview:label];
    
    //添加1-9数字
    NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    int n=0;
    //九个数字 三乘三排列
    for (int i=0; i<3; i++)
    {
        for (int j=0; j<3; j++)
        {
            self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            self.button.frame=CGRectMake(30+65*j, 280-65*i, 60, 60);
            [self.button setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal];
            [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.view addSubview:button];
            [self.button addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    //单独添加0，因为0的位置和其他九个不一样
    UIButton *button0=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [button0 setFrame:CGRectMake(30, 345, 120, 60)];
    
    [button0 setTitle:@"0" forState:UIControlStateNormal];
    
    [button0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [button0 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button0];
    
    
    //添加运算符
    NSArray *array1=[NSArray arrayWithObjects:@"+",@"-",@"*",@"/",nil];
    for (int i=0; i<4; i++)
    {
        UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button1 setFrame:CGRectMake(225, 280-65*i, 60, 60)];
        [button1 setTitle:[array1 objectAtIndex:i] forState:UIControlStateNormal];
        [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button1];
    }
    
    //添加=
    
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [button2 setFrame:CGRectMake(225, 345, 60, 60)];
    
    [button2 setTitle:@"=" forState:UIControlStateNormal];
    
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [button2 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    //添加清除键
    
    UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button3 setFrame:CGRectMake(30, 85, 60, 60)];
    [button3 setTitle:@"AC" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    //添加.
    
    UIButton *button4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button4 setFrame:CGRectMake(160, 345, 60, 60)];
    [button4 setTitle:@"." forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    //添加＋／－键
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button5 setFrame:CGRectMake(95, 85, 60, 60)];
    [button5 setTitle:@"+/-" forState:UIControlStateNormal];
    [button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button5 addTarget:self action:@selector(zf:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button5];
    
    //添加％键
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button6 setFrame:CGRectMake(160, 85, 60, 60)];
    [button6 setTitle:@"%" forState:UIControlStateNormal];
    [button6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button6 addTarget:self action:@selector(percent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button6];
    
    self.string=[[NSMutableString alloc]init];
}

-(void)one:(id)sender
{
    if ([self.string hasPrefix:@"+"]||[self.string hasPrefix:@"-"]||[self.string hasPrefix:@"*"]||[self.string hasPrefix:@"/"])
    {
        [self.string setString:@""];
    }
    [self.string appendString:[sender currentTitle]];
    self.label.text=[NSString stringWithString:string];
    self.num1=[self.label.text doubleValue];
    self.num5=self.num1;
    
}

-(void)two:(id)sender
{
    NSString *shuzi = [NSString stringWithFormat:@"%f",self.num1];
    [self.string setString:@""];
    [self.string appendString:[sender currentTitle]];
    self.label.text=[NSString stringWithString:shuzi];
    
    if ([self.string hasPrefix:@"+"])
    {
        self.num2=self.num1;
        self.num4=1;
    }
    
    else if([self.string hasPrefix:@"-"])
    {
        self.num2=self.num1;
        self.num4=2;
    }

    else if([self.string hasPrefix:@"*"])
    {
        self.num2=self.num1;
        self.num4=3;
    }

    else if([self.string hasPrefix:@"/"])
    {
        self.num2=self.num1;
        self.num4=4;
    }
}


-(void)go:(id)sender
{
    //判断输入是+号
    if (self.num4==1)
    {
        self.num3=self.num2+[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%f",self.num3];
        self.num2=self.num5;
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    //判断输入是-号
    else if(self.num4==2)
    {
        self.num3=self.num2-self.num5;
        self.label.text=[NSString stringWithFormat:@"%f",self.num3];
        self.num2=self.num3;
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    //判断输入是*号
    else if(self.num4==3)
    {
        self.num3=self.num2*[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%f",self.num3];
        self.num2=self.num5;
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    //判断输入是/号
    else if(self.num4==4)
    {
        self.num3=self.num2/self.num5;
        self.label.text=[NSString stringWithFormat:@"%f",self.num3];
        self.num2=self.num3;
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    else if(!self.num4)
    {
        self.label.text=[NSString stringWithFormat:@"%f",self.num1];
        [self.string setString:@""];
    }
}

//当按下清除建时，所有数据清零
-(void)clean:(id)sender{
    [self.string setString:@""];
    self.num3=0;
    self.num2=0;
    self.label.text=@"0";
    
}

// ＋／－键方法
-(void)zf:(id)sender
{
    if([self.label.text doubleValue]<0)
    {
        self.num3 = fabs([self.label.text doubleValue]);
        self.label.text = [NSString stringWithFormat:@"%f",self.num3];
    }
    else if([self.label.text doubleValue]>0)
    {
        self.num3 = 0 - [self.label.text doubleValue];
        self.label.text = [NSString stringWithFormat:@"%f",self.num3];
    }
}

//%键方法
-(void)percent:(id)sender
{
    self.num3 = self.num1/100;
    self.label.text = [NSString stringWithFormat:@"%f",self.num3];
    self.num1 = self.num3;
    self.num3 = 0;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
